package com.sentiment.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.bson.Document;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sentiment.config.Config;
import com.sentiment.crawler.BaiduRelativeCrawler;
import com.sentiment.database.bean.Keyword;
import com.sentiment.database.bean.UserInfo;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.dbutil.UtilDao;
import com.sentiment.shiro.ActiveUser;

/**
 * Servlet implementation class CorrelationInfo
 */
@SuppressWarnings("serial")
@WebServlet("/CorrelationInfo")
public class CorrelationInfoServlet extends HttpServlet {
	private static final Logger LOG = LoggerFactory.getLogger(CorrelationInfoServlet.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CorrelationInfoServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			LOG.error(e.toString());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	
	
	
	public void correlationInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());
		
		String keyword = request.getParameter("wd");
		// String charset = CharsetDetector.guessEncoding(keyword.getBytes());
		// keyword = new String(keyword.getBytes(), charset);
		keyword = URLDecoder.decode(keyword, "UTF-8");

		Subject subject = SecurityUtils.getSubject();
		ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
		String user_id;
		if (activeUser == null) {
			

		} else {
			user_id = activeUser.getUser_id();
			UtilDao dao = new UtilDao();
			dao.saveKeyword2user(user_id, keyword);
		}
		// AlgoHttp algo = new AlgoHttp();
		// List<Keywords> list = algo.suggest(keyword);
		BaiduRelativeCrawler brc = new BaiduRelativeCrawler(Config.crawlerDataPath);
		brc.getSearch(keyword);
		List<JSONObject> list = brc.getList();

		StringBuilder sbd = new StringBuilder();
		sbd.append("{\"correlationWords\":" + list + "}");
		response.setCharacterEncoding("utf-8");
		response.getWriter().print(sbd.toString());
	}

}
